package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import com.example.utils.SortedListOf;

import java.util.Random;

import org.testng.annotations.Test;

public class ContactModificationTests extends TestBase {

	@Test(dataProvider = "randomValidContactGenerator")
	
	public void modifySomeContact(ContactData contact) {
		//save old state
		SortedListOf<ContactData> oldList = app.getContactHelper().getContacts();
        
        Random rnd = new Random();
        int index = oldList.size()-1;
        if (index > 0) {
        	index = rnd.nextInt(index);
        }
        
        // --------------  actions ----------------
        // old variant of contact modification
        //app.getContactHelper().modifyContact(index, contact);
        
        // get contact from cache by index
        ContactData contactFromCache = app.getContactHelper().getContactByIndex(index);
        // open contact modification form
        app.getContactHelper().initContactModification(index);
        // read contact data from form
        ContactData uiContact = app.getContactHelper().getCurrentUiContact();
        // complete contact modification
        app.getContactHelper().completeContactModification(contact);
              		
		//save new state
        SortedListOf<ContactData> newList = app.getContactHelper().getContacts();
        
        // compare states 
        assertThat(newList, equalTo(oldList.without(index).withAdded(contact))); 
        // compare contact data from UI and cache 
        assertThat(contactFromCache, equalTo(uiContact)); 
	}
}
